package com.caine.tool;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;

/**
 * 数字转换工具
 */
public class NumberTool {
    /**
     * int转换
     * @param val
     * @param toLE
     * @return
     */
    public static String int2hex(int val, boolean toLE) {
        ByteBuffer byteBuffer = ByteBuffer.allocate(4);
        byte[] parts = new byte[4];
        if (toLE) {
            byteBuffer.order(ByteOrder.LITTLE_ENDIAN);
        }
//        parts[0] = (byte) ((val >> 24) & 0xff);
//        parts[1] = (byte) ((val >> 16) & 0xff);
//        parts[2] = (byte) ((val >> 8) & 0xff);
//        parts[3] = (byte) (val & 0xff);
        byteBuffer.putInt(val);
        byteBuffer.flip();
        byteBuffer.get(parts);
        StringBuilder sb = new StringBuilder();
        for (byte b : parts) {
            sb.append(fillData(Integer.toHexString(b & 0xFF)));
        }
        return sb.toString();
    }


    public static int hex2Int(String hex, boolean toLE) {
        String[] parts = hex.split("(?<=\\G.{2})");
        ByteBuffer byteBuffer = ByteBuffer.allocate(4);
        if (toLE) {
            byteBuffer.order(ByteOrder.LITTLE_ENDIAN);
        }
        for (String part : parts) {
            byteBuffer.put((byte) (Integer.parseInt(part, 16) & 0XFF));
        }
        byteBuffer.flip();
        return byteBuffer.getInt();
    }

    public static double hex2Double(String hex, boolean toLE) {
        String[] parts = hex.split("(?<=\\G.{2})");
        ByteBuffer byteBuffer = ByteBuffer.allocate(8);
        if (toLE) {
            byteBuffer.order(ByteOrder.LITTLE_ENDIAN);
        }
        for (String part : parts) {
            byteBuffer.put((byte) (Integer.parseInt(part, 16) & 0XFF));
        }
        byteBuffer.flip();
        return byteBuffer.getDouble();
    }

    //
    public static String double2hex(double val, boolean toLE) {
        ByteBuffer buffer = ByteBuffer.allocate(8);
        if (toLE) {
            buffer.order(ByteOrder.LITTLE_ENDIAN);
        }
        buffer.putDouble(val);
        buffer.flip();
        byte[] bytes = new byte[8];
        buffer.get(bytes);
        StringBuffer sb = new StringBuffer();
        for (byte aByte : bytes) {
            sb.append(fillData(Integer.toHexString(aByte & 0xFF)));
        }
        return sb.toString();
    }

    public static String int2hex(int val) {
        return int2hex(val, false);
    }

    public static String int2hexLE(int val) {
        return int2hex(val, true);
    }

    public static String fillData(String val) {
        return StringTool.fillData(val, 2, "0");
    }

    public void test(String test) {
        System.out.println("says:" + test);
    }

    public static void main(String[] args) throws Exception {
//        ByteBuffer bb = ByteBuffer.allocate(4);
//        bb.order(ByteOrder.BIG_ENDIAN);
        System.out.println(int2hexLE(255));
        System.out.println(int2hex(255));
//        Class zlass = Class.forName("com.caine.tool.NumberTool");
//        Method method = zlass.getMethod("fillData", String.class);
//        Object o = zlass.newInstance();
//        method.setAccessible(true);
//        Object a = method.invoke(o, "a");
//        System.out.println(a);
        int wan = 10000;
        System.out.println(int2hex(10000, true));
        System.out.println(hex2Int("10270000", true));

        System.out.println(int2hex(10000, false));
        System.out.println(hex2Int("00002710", false));


        System.out.println(double2hex(3.1415926, true));
        System.out.println(hex2Double("4ad8124dfb210940", true));
        System.out.println(double2hex(3.1415926, false));
        System.out.println(hex2Double("400921fb4d12d84a", false));


    }
}
